package com.etc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.etc.entity.BroadcastingStation;
import com.etc.entity.MusicInfo;
import com.etc.entity.PageInfo;
import com.etc.service.BroadcastingStationService;
import com.etc.service.impl.BroadcastingStationServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;


/**
 * Servlet implementation class BroadcastingStation
 */
@WebServlet("/broadcastingStations")
@MultipartConfig
public class BroadcastingStationsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private BroadcastingStationService broadcastingStationService = new BroadcastingStationServiceImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BroadcastingStationsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 接收请求参数op,用于区分不同的请求
		String op = request.getParameter("op");
		if ("Broadcasting_station".equals(op)) {
			dofindBroadcastingStation(request,response);		
		}
		if ("BroadcastingMusicId".equals(op)) {
			doGetInRadio(request,response);
		}
		if ("pageByCondition".equals(op)) {
			doFindByCondition(request, response);
		}
		if ("SongRecommend".equals(op)) {
			doGetSongRecommend(request,response);		
		}
		
		if ("songSheet".equals(op)) {
			doFindByUserId(request, response);
		}

		if ("detail".equals(op)) {
			doFindByBroadcastingStationId(request, response);
		}


	}
	
	
	private void doGetSongRecommend(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 替换首页中歌单推荐的五种场景
				//接收请求参数
				//zt表示前端传递的音乐类型
				String zt = request.getParameter("zt");
				//调用业务层
				 List<BroadcastingStation> list = broadcastingStationService.getBroadcastingStations(zt);
				
				// 利用第三方类库jackson将响应数据转换成JSON格式
					//设置响应的内容类型
					response.setCharacterEncoding("UTF-8");
					response.setContentType("application/json;charset=utf-8");
					PrintWriter out = response.getWriter();
					ObjectMapper objectMapper = new ObjectMapper();
					String json = objectMapper.writeValueAsString(list);
					out.print(json);
						
	}

	private void doFindByUserId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userId = request.getParameter("userId");
		List<BroadcastingStation> broadcastingStations = broadcastingStationService.findByUserId(Integer.parseInt(userId));
		// 响应
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(broadcastingStations);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		
	}

	private void doGetInRadio(HttpServletRequest request, HttpServletResponse response) throws IOException {
				//接收请求参数
				//获取要播放音乐的id
				Integer BroadcastingStationId = Integer.valueOf(request.getParameter("MI"));
				//设置响应的内容类型
				response.setCharacterEncoding("UTF-8");
				response.setContentType("application/json;charset=utf-8");
				PrintWriter out = response.getWriter();
				//调用业务层
				int n = broadcastingStationService.getMusicByCondition(BroadcastingStationId);
				// 利用第三方类库jackson将响应数据转换成JSON格式
				ObjectMapper objectMapper = new ObjectMapper();
				String json = objectMapper.writeValueAsString(n);
				out.print(json);
	}

	private void dofindBroadcastingStation(HttpServletRequest request, HttpServletResponse response) throws IOException {
				//接收请求参数
				String zt = request.getParameter("zt");
								
				//设置响应的内容类型
				response.setCharacterEncoding("UTF-8");
				response.setContentType("application/json;charset=utf-8");
				PrintWriter out = response.getWriter();
				//调用业务层
				 List<BroadcastingStation> list = broadcastingStationService.getBroadcastingStations(zt);
				 
				// 利用第三方类库jackson将响应数据转换成JSON格式
				ObjectMapper objectMapper = new ObjectMapper();
				String json = objectMapper.writeValueAsString(list);
				out.print(json);
	}

	private void doFindByCondition(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 接收请求参数
		String languagesName= request.getParameter("languagesName");
		String styleName= request.getParameter("styleName");
		String themeName= request.getParameter("themeName");
		String moodName= request.getParameter("moodName");
		String sceneName= request.getParameter("sceneName");
		String pageNum= request.getParameter("pageNum");
		int pageSize = 10;
		// 调用业务层
		
		PageInfo<BroadcastingStation> pageInfo = broadcastingStationService.findBroadcastingStationsByCondition(languagesName, styleName, themeName, moodName, sceneName, Integer.parseInt(pageNum), pageSize);
		
		// 响应
		// 利用第三方库jackson将Java对象转换成JSON格式
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(pageInfo);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		
		
	}
	private void doFindByBroadcastingStationId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 接收请求参数
		String broadcastingStationId = request.getParameter("broadcastingStationId");
		
		// 调用业务层
		
		BroadcastingStation broadcastingStation = broadcastingStationService.getBroadcastingStationById(Integer.parseInt(broadcastingStationId));
		
		// 响应
		// 利用第三方库jackson将Java对象转换成JSON格式
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(broadcastingStation);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		
		
	}
}
